var write = require('hy5b/backend/write');
var moment = require('moment');

function fileName() {
    var nm = moment();
    return ['dest','百度云同步盘', '同步', nm.year(), nm.month() + 1, nm.date() + nm.unix() + '.html'].join('/');
}

var ff = write(fileName());

var readFile = require('utils-fs-read-file');

function read(file) {
    var out = readFile.sync(file);
    if (out instanceof Error) {
        throw out;
    }
    return out;
}

module.exports = function(data) {
    ff.write('<!DOCTYPE html>');
    ff.write('<html>');
    ff.write('<head>');
    ff.write('<meta charset="utf-8">');
    ff.write('<meta name="viewport" content="width=1024">');
    ff.write('<title>' + fileName() + '</title>');
    ff.write('<script>');
    ff.write(read('./extra/standalone-framework.js'));
    ff.write(read('./extra/highstock.js'));
    ff.write('</script>');

    ff.write(read('./extra/snippet.html'));

    ff.write('<table cellpadding="0" cellspacing="0" style="padding-top:30px;">');
    for (var i = 0; i < data.length; i++) {
        ff.write('<tr>');
        ff.write('<td width="10%">' + data[i].show + '</td>');
        ff.write('<td width="10%">' + data[i].mdup + '</td>');
        ff.write('<td width="10%">' + data[i].md2up + '</td>');
        ff.write('<td width="10%">' + (data[i].mwup || '') + '</td>');
        ff.write('<td width="10%">' + (data[i].mw2up || '') + '</td>');
        ff.write('<td width="10%">' + data[i].close + '</td>');
        ff.write('<td width="40%">');
        ff.write('<div id="k' + i + '" style="height:300px;width:400px;"></div>');
        ff.write('<script type="text/javascript">');
        ff.write('c("k' + i + '", ' + JSON.stringify(data[i].ohlc) + ',' + JSON.stringify(data[i].macd) + ');');
        ff.write('</script>');
        ff.write('</td>');
        ff.write('</tr>');
    }
    ff.write('</table>');
    ff.write('<script>');
    ff.write(read('./extra/app.js'));
    ff.write('</script>');
    ff.write('</body>');
    ff.write('</html>');
}
